package com.xiehua.task.server.handler;

import com.xiehua.task.intf.AbsBusinessHandler;
import com.xiehua.task.job.bucket.CronBucket;
import com.xiehua.task.job.bucket.TopicBucket;
import com.xiehua.task.msg.BindClientBody;
import com.xiehua.task.msg.FixedTaskMetaData;
import com.xiehua.task.protocol.TaskPacket;
import com.xiehua.task.redis.RedisTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.core.ChannelContext;
import redis.clients.jedis.Jedis;

import java.util.List;

public class BindClientHandler extends AbsBusinessHandler<BindClientBody>{

    private static Logger log = LoggerFactory.getLogger(TaskRegReqHandler.class);

    @Override
    public Class<BindClientBody> bodyClass() {
        return BindClientBody.class;
    }

    @Override
    public Object handler(TaskPacket packet, BindClientBody bsBody, ChannelContext channelContext) throws Exception {
        log.info("客户端:{}上线了",bsBody.getTopicName());
        Jedis jedis = RedisTemplate.getJedis();

        TopicBucket topicBucket = new TopicBucket(jedis);
        topicBucket.addTopicInfo(bsBody.getTopicName(),channelContext.getClientNode(),packet.getClientId(),channelContext.getGroupContext());
        List<FixedTaskMetaData> fixedTaskMetaData = bsBody.getFixedTaskMetaData();
        if(fixedTaskMetaData != null && fixedTaskMetaData.size() > 0) new CronBucket(jedis).updateTopicCron(bsBody.getTopicName(),fixedTaskMetaData);

        jedis.close();
        return null;
    }
}
